
const _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

exports.__esModule = true;
exports.default = void 0;

const _extends2 = _interopRequireDefault(require('@babel/runtime/helpers/extends'));

const _utils = require('../utils');

const _string = require('../utils/format/string');

const _number = require('../utils/format/number');

const _shared = require('./shared');

const _createNamespace = (0, _utils.createNamespace)('time-picker');
const createComponent = _createNamespace[0];

const _default = createComponent({
  mixins: [_shared.TimePickerMixin],
  props: (0, _extends2.default)({}, _shared.sharedProps, {
    minHour: {
      type: [Number, String],
      default: 0,
    },
    maxHour: {
      type: [Number, String],
      default: 23,
    },
    minMinute: {
      type: [Number, String],
      default: 0,
    },
    maxMinute: {
      type: [Number, String],
      default: 59,
    },
  }),
  computed: {
    ranges: function ranges() {
      return [{
        type: 'hour',
        range: [+this.minHour, +this.maxHour],
      }, {
        type: 'minute',
        range: [+this.minMinute, +this.maxMinute],
      }];
    },
  },
  watch: {
    filter: 'updateInnerValue',
    minHour: 'updateInnerValue',
    maxHour: 'updateInnerValue',
    minMinute: 'updateInnerValue',
    maxMinute: 'updateInnerValue',
    value: function value(val) {
      val = this.formatValue(val);

      if (val !== this.innerValue) {
        this.innerValue = val;
        this.updateColumnValue();
      }
    },
  },
  methods: {
    formatValue: function formatValue(value) {
      if (!value) {
        value = `${(0, _string.padZero)(this.minHour)}:${(0, _string.padZero)(this.minMinute)}`;
      }

      const _value$split = value.split(':');
      let hour = _value$split[0];
      let minute = _value$split[1];

      hour = (0, _string.padZero)((0, _number.range)(hour, this.minHour, this.maxHour));
      minute = (0, _string.padZero)((0, _number.range)(minute, this.minMinute, this.maxMinute));
      return `${hour}:${minute}`;
    },
    updateInnerValue: function updateInnerValue() {
      const _this$getPicker$getIn = this.getPicker().getIndexes();
      const hourIndex = _this$getPicker$getIn[0];
      const minuteIndex = _this$getPicker$getIn[1];

      const _this$originColumns = this.originColumns;
      const hourColumn = _this$originColumns[0];
      const minuteColumn = _this$originColumns[1];
      const hour = hourColumn.values[hourIndex] || hourColumn.values[0];
      const minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
      this.innerValue = this.formatValue(`${hour}:${minute}`);
      this.updateColumnValue();
    },
    onChange: function onChange(picker) {
      const _this = this;

      this.updateInnerValue();
      this.$nextTick(() => {
        _this.$nextTick(() => {
          _this.$emit('change', picker);
        });
      });
    },
    updateColumnValue: function updateColumnValue() {
      const _this2 = this;

      const { formatter } = this;
      const pair = this.innerValue.split(':');
      const values = [formatter('hour', pair[0]), formatter('minute', pair[1])];
      this.$nextTick(() => {
        _this2.getPicker().setValues(values);
      });
    },
  },
});

exports.default = _default;
